﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 静态的，用字符表示的地图
/// </summary>
public struct StaticMapState
{
    public int SizeX;
    public int SizeY;
    public char[] Map;
}

public class MapCellRuntimeObject : ICloneable
{
    public string Name;
    public Sprite Sprite;
    public ImpulseType Impulse;
    public GameObject ObjectRef;

    public MapCellRuntimeObject()
    {
        Impulse = ImpulseType.None;
    }
    public object Clone()
    {
        var RuntimeObject = new MapCellRuntimeObject();

        RuntimeObject.Name = Name;
        RuntimeObject.Sprite = Sprite;
        RuntimeObject.Impulse = Impulse;
        RuntimeObject.ObjectRef = ObjectRef;

        return RuntimeObject;
    }
}
public struct MapCell
{
    public List<MapCellRuntimeObject> Objects;
}

/// <summary>
/// 动态的，存在Object实例与Impulse状态的地图
/// </summary>
public struct MapState : ICloneable
{
    public int SizeX;
    public int SizeY;
    public MapCell[] Map;

    static public MapState GenerateByStatic(in StaticMapData StaticMapData)
    {
        // 初始化数据
        var OutMapState = new MapState();
        OutMapState.SizeX = StaticMapData.StaticMapState.SizeX;
        OutMapState.SizeY = StaticMapData.StaticMapState.SizeY;
        OutMapState.Map = new MapCell[OutMapState.SizeX * OutMapState.SizeY];

        // 点对点生成Runtime Object
        for (int MapIndex = 0; MapIndex < OutMapState.SizeY * OutMapState.SizeX; MapIndex++)
        {
            Vector2Int Point = new Vector2Int(MapIndex % OutMapState.SizeX, MapIndex / OutMapState.SizeX);
            // 初始化Object列表
            OutMapState.Map[MapIndex].Objects = new List<MapCellRuntimeObject>();
            var CellObjects = OutMapState.Map[MapIndex].Objects;

            // 拿到Static Map上面的信息
            var Symbol = StaticMapData.StaticMapState.Map[MapIndex];
            string Name;
            if (!StaticMapData.SymbolToName(Symbol, out Name)) { return OutMapState; }

            if (Name == "Ground") { continue; }
            PuzzleObject Attr;
            if (!StaticMapData.NameToAttr(Name, out Attr)) { return OutMapState; }

            // 将规则设置的信息装载到Object
            var Element = new MapCellRuntimeObject();
            Element.Name = Name;
            Element.Sprite = Attr.Sprite;
            CellObjects.Add(Element);   
        }

        return OutMapState;
    }

    /// <summary>
    /// 刷新状态
    /// </summary>
    public void RefreshStatus()
    {
        for (int MapIndex = 0; MapIndex < SizeY * SizeX; MapIndex++)
        {
            Map[MapIndex]
                .Objects
                .ForEach(a => a.Impulse = ImpulseType.None);
        }
    }

    public object Clone()
    {
        var OutMapState = new MapState();
        OutMapState.SizeX = SizeX;
        OutMapState.SizeY = SizeY;
        OutMapState.Map = new MapCell[OutMapState.SizeX * OutMapState.SizeY];

        for (int MapIndex = 0; MapIndex < SizeY * SizeX; MapIndex++)
        {
            OutMapState.Map[MapIndex].Objects = new List<MapCellRuntimeObject>();
            var Objects = OutMapState.Map[MapIndex].Objects;

            foreach (var Element in Map[MapIndex].Objects)
            {
                Objects.Add((MapCellRuntimeObject)Element.Clone());
            }
        }

        return OutMapState;
    }
}